/**
 * 初始化图片管理详情对话框
 */
var PictureInfoDlg = {
    pictureInfoData : {}
};

/**
 * 清除数据
 */
PictureInfoDlg.clearData = function() {
    this.pictureInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PictureInfoDlg.set = function(key, val) {
    this.pictureInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PictureInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
PictureInfoDlg.close = function() {
    parent.layer.close(window.parent.Picture.layerIndex);
}

/**
 * 收集数据
 */
PictureInfoDlg.collectData = function() {
    this
    .set('id')
    .set('name')
    .set('type')
    .set('link')
    .set('url')
    .set('orderval')
    .set('createTime')
    .set('updateTime')
    .set('isDel');
}

/**
 * 提交添加
 */
PictureInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/picture/add", function(data){
        Feng.success("添加成功!");
        window.parent.Picture.table.refresh();
        PictureInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.pictureInfoData);
    ajax.start();
}

/**
 * 加载类别选项
 */
PictureInfoDlg.getAllCategories = function() {
    $("#category").empty();
    var selected = $("#categoryValue").val();
    $.ajax({
        url:"/pictureCategory/getAllCategories" ,
        type:"get",
        dataType:"json",
        success:function(data){
            var dataList = eval(data);
            for(var i = 0;i<dataList.length;i++){  //循环LIST
                var id = dataList[i].id;
                var name = dataList[i].name;
                if (id == selected) {
                    $("#category").append("<option value='"+id+"' selected='selected'>"+name+"</option>");
                }else{
                    $("#category").append("<option value='"+id+"'>"+name+"</option>");
                }
            }
        },
        error:function(XMLHttpRequest,textStatus, errorThrown) {
            // alert(errorThrown);
        }
    });
}


/**
 * 提交修改
 */
PictureInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/picture/update", function(data){
        Feng.success("修改成功!");
        window.parent.Picture.table.refresh();
        PictureInfoDlg.close();
    },function(data){
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.pictureInfoData);
    ajax.start();
}

$(function() {

    //初始化“是否置顶”选项
    $("#isDel").val($("#isDelValue").val());

    // 初始化文章附带图片上传
    var pictureUp = new $WebUpload("picture");
    pictureUp.init();

    // 初始化文章类别下拉框
    NewsInfoDlg.getAllCategories();
    $("#category").val($("#categoryValue").val());

});
